package com.eric.ztea.controller;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eric.ztea.dao.EquipmentRepository;
import com.eric.ztea.domain.Equipment;

@RestController
@RequestMapping("/api/equipment")
public class EquipmentController {

	@Autowired
    EquipmentRepository equipmentRepository;	
	
	@GET
	@RequestMapping(value = "/findById")
	public Equipment findById(@RequestParam(value = "id", required = true) Long id) {
		return equipmentRepository.findById(id);
	}

	@GET
	@RequestMapping(value = "/findByName")
	public Equipment findByName(@RequestParam(value = "name", required = true) String name) {
		return equipmentRepository.findByName(name);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public Equipment save(@RequestParam(value = "id", required = false) Long id,			
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "location", required = false) String location,
			@RequestParam(value = "longitude", required = false) Double longitude,
			@RequestParam(value = "latitude", required = false) Double latitude,
			@RequestParam(value = "cityid", required = false) Long cityid,
			@RequestParam(value = "status", required = false) Long status,
			@RequestParam(value = "type", required = false) Long type,
			@RequestParam(value = "description", required = false) String description) {

		return equipmentRepository.save(new Equipment(id, name, location, longitude, latitude, cityid, status, type, description));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		equipmentRepository.delete(id);
	}
	
}
